package com.xhh.enjoyapp.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xhh.enjoyapp.Bean.Community;
import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * @description 添加论坛
 * @author: weiXhh
 * @create: 2020-05-15 12:29
 **/
public class PushCommunity extends AppCompatActivity {

    private ImageView back;
    private EditText comName, comInfo;
    private Button push;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pushcommunity);

        initView();

        back.setOnClickListener(click->{
            finish();
        });

        // 创建论坛
        push.setOnClickListener(click->{
            User user = BmobUser.getCurrentUser(User.class);
            Community community = new Community();
            community.setName(comName.getText().toString());
            community.setInfo(comInfo.getText().toString());
            community.setAuthor(user);
            community.setOwner(user.getUsername());

            community.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if(e == null){
                        Toast.makeText(PushCommunity.this, "创建成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(PushCommunity.this, "创建使用", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        });
    }

    private void initView() {
        back = findViewById(R.id.push_community_back);
        comName = findViewById(R.id.push_community_name_et);
        comInfo = findViewById(R.id.push_community_info_et);
        push = findViewById(R.id.push_community_btn);

    }
}
